#ifndef _M_YMODEM_H
#define _M_YMODEM_H
/**************************************************************************************************
 *                                            INCLUDES
 **************************************************************************************************/
#include <zephyr/kernel.h>
# include <stdio.h>
#include <stdlib.h>
#include <zephyr/drivers/uart.h>
#include <ctype.h>
#include <zephyr/sys/util.h>
#include <zephyr/device.h>
#include <zephyr/shell/shell_uart.h>
#include <string.h>
#include <zephyr/drivers/flash.h>
#include <zephyr/devicetree.h>
/*********************************************************************
 * CONSTANTS
 */
#define SOH                     (0x01)  /* start of 128-byte data packet */
#define STX                     (0x02)  /* start of 1024-byte data packet */
#define EOT                     (0x04)  /* end of transmission */
#define ACK                     (0x06)  /* acknowledge */
#define NAK                     (0x15)  /* negative acknowledge */
#define CA                      (0x18)  /* two of these in succession aborts transfer */
#define CRC16                   (0x43)  /* 'C' == 0x43, request 16-bit CRC */

#define ABORT1                  (0x41)  /* 'A' == 0x41, abort by user */
#define ABORT2                  (0x61)  /* 'a' == 0x61, abort by user */
#define ENTER                   (0xd)  /*  enter , do not jump to app , done in 2S*/

#define NAK_TIMEOUT             (0x100000)
#define MAX_ERRORS              (5)
//传输的数据包的大小
#define PACKET_SIZE             (128)
#define PACKET_1K_SIZE          (1024)
//包的数据序号，以及比较缓存序号，用于确保数据包的序号
#define PACKET_SEQNO_INDEX      (1)
#define PACKET_SEQNO_COMP_INDEX (2)
//数据中的文件头，以及文件信息的在帧中占位长度
#define PACKET_HEADER           (3)
#define PACKET_TRAILER          (2)
#define PACKET_OVERHEAD         (PACKET_HEADER + PACKET_TRAILER)

  
#define FILE_NAME_LENGTH (256)  
#define FILE_SIZE_LENGTH (16)  

#define PAGE_SIZE (1024)

#define FLASH_BASE_ADDRESS 0x08000000
#define APP_BANK1_ADDRESS 0x08020000
#define APP_BANK2_ADDRESS 0x08060000
#define APP_FLASH_SIZE    256*1024

//FLASH 扇区的起始地址
#define ADDR_FLASH_SECTOR_0     ((uint32_t)0x08000000) 	//扇区0起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_1     ((uint32_t)0x08004000) 	//扇区1起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_2     ((uint32_t)0x08008000) 	//扇区2起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_3     ((uint32_t)0x0800C000) 	//扇区3起始地址, 16 Kbytes  
#define ADDR_FLASH_SECTOR_4     ((uint32_t)0x08010000) 	//扇区4起始地址, 64 Kbytes  
#define ADDR_FLASH_SECTOR_5     ((uint32_t)0x08020000) 	//扇区5起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_6     ((uint32_t)0x08040000) 	//扇区6起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_7     ((uint32_t)0x08060000) 	//扇区7起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_8     ((uint32_t)0x08080000) 	//扇区8起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_9     ((uint32_t)0x080A0000) 	//扇区9起始地址, 128 Kbytes  
#define ADDR_FLASH_SECTOR_10    ((uint32_t)0x080C0000) 	//扇区10起始地址,128 

/*********************************************************************
 * TYPE_DEFS
 */

/*********************************************************************
 * FUNCTIONS
 *********************************************************************/
int32_t Ymodem_Receive(uint8_t *buf,uint32_t addrDst);
int32_t SerialDownload(uint32_t addrDst);
uint32_t iap_erase_flash(uint32_t addrDst, uint32_t length);
uint32_t iap_write_flash(uint32_t addrDst, uint8_t *addrSrc, uint32_t length);

#endif    //_M_YMODEM_H
